// pages/dayQuestion/dayQuestion/dayQuestion.js
const app = getApp();
import Api from '../../../utils/http';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    answerList: [],
    answer: '',
    yes: [],
    zimu: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', ],
    chooseAnswer: '', //选择的答案
    state: '', //0为未答题，1为已答题
    answerindex:'',//正确答案
    text:'',//选着文字
    chooseIndex: '',//选择的索引
    answertest:'',//提交的答案
  },
  //选择答案
  chooseAnswer: function (e) {
    var that = this;
    console.log(that.data)
    var _index = e.currentTarget.dataset.index;
    console.log(_index);

    var yes = [];
    var answerList = that.data.answerList;
    var zimu = that.data.zimu;
    var chooseAnswer = '';
    var text='';
    for (var i = 0; i < answerList.length; i++) {
      if (i == _index) {
        answerList[i].choose = true;
        yes = answerList[i];
        yes['index'] = i;
        chooseAnswer = zimu[i];
        text=answerList[i].title;
        console.log(yes.title)
      } else {
        answerList[i].choose = false;
      }
    }
    that.setData({
      answerList: answerList,
      yes: answerList.title,
      chooseAnswer: chooseAnswer,
      text:text,
      chooseIndex: _index,
    })
  },
  //跳转 答题排行榜
  toRanking: function () {
    wx.navigateTo({
      url: '/pages/dayQuestion/questionRanking/questionRanking',
    })
  },
  //答题提交
  onSubimt: function (e) {
    var that = this;
    // let tid = that.data.chooseAnswer;
    var chooseIndex = that.data.chooseIndex;
    if(chooseIndex == null){
      wx.showToast({
        icon: 'none',
        title: '请选择答案！',
      })
      return;
    }
    let uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      problem_id: that.data.answer.id,
      answer_res: chooseIndex+1,
    }
    console.log(param)
    Api.ajax("/app/Zarticle/answerProblem", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        wx.showToast({
          title: '提交成功',
          icon:"none",
          duration:1000,
        })
        that.setData({
          state:1,
        })
        that.onShow();
      }else{
        wx.showToast({
          title: '提交失败',
          icon:"none",
          duration:1000,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(that.data)
    that.setData({
      options: options,
      state: options.states
    })
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that =this;
    var state= that.data.state
    console.log(state)
    if (state==1) {
      that.getAnswerStatus();
    } else if (state == 0){
      that.getProblemList();
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  //已答题状态
  getAnswerStatus: function () {
    var that = this;
    let uid = wx.getStorageSync('userid')
    var param = {
      uid: uid.id
    }
    console.log(param)
    Api.ajax('/app/Zarticle/todayProblem', param).then(res => {
      console.log(res)
      if (res.code == 200) {
        wx.showToast({
          title: '今日答题已完成',
          icon:'none',
          duration: 3000,
        })
        if (res.data.answer_res==1) {
          that.setData({
            answertest: 'A'
          })
        }else if (res.data.answer_res==2) {
          that.setData({
            answertest: 'B'
          })
        }else if (res.data.answer_res==3) {
          that.setData({
            answertest: 'C'
          })
        }else if (res.data.answer_res==4) {
          that.setData({
            answertest: 'D'
          })
        };
        if (res.data.is_sure==1) {
          that.setData({
            answerindex: 'A'
          })
        }else if (res.data.is_sure==2) {
          that.setData({
            answerindex: 'B'
          })
        }else if (res.data.is_sure==3) {
          that.setData({
            answerindex: 'C'
          })
        }else {
          
          that.setData({
            answerindex: 'D'
          })
        }
        that.setData({
          answer: res.data,
          answerList: res.data.xuan_json
        })
      }
    })
  },
  //未答题接口
  getProblemList: function () {
    var that = this;
    let uid = wx.getStorageSync('userid')
    var param = {
      uid: uid.id
    }
    Api.ajax("/app/Zarticle/problemList", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          answer: res.data,
          answerList: res.data.xuan_json
        });
      }
    })
  },
  

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})